using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;
using Newtonsoft.Json;

namespace Easily
{

    ///==============================================================================
    /// <summary>
    /// 加载配置文件“Config.json”的信息，并解析出控件图标信息和可扩展硬件连接信息
    /// IconInfoSize指定
    /// </summary>
    ///==============================================================================
    public class LoadConfig
    {
        #region 与“Config.json”文件相关的数据类型信息
        [Serializable]
        public class MCU_CONFIG
        {
            public string name;
            public string type;
        }
        [Serializable]
        public class PARAM_VALUE
        {
            public string name;
            public string description;
        }
        [Serializable]
        public class FUN
        {
            public string description;
            public string name;
            public string return_type;
            public string param_type;
            public List<PARAM_VALUE> param_values;
        }
        [Serializable]
        public class Control
        {
            public string name;
            public string type;
            public string image1;
            public string image2;
            public string image3;
            public string hard_type;
            public string head_file;
            public string init_fun;
            public List<FUN> funs;
        }
        [Serializable]
        public class Config
        {
            public MCU_CONFIG mcu_config;
            //[2021/1/30 把hard_connect保存到工程文件也就是xml文件中
            //public HARD_CONNECT hard_connect;
            public List<Control> controls;
        }
        #endregion

        #region IconBoxInfo类的字段
        ///==============================================================================
        //使用字典存储控件的信息，关键字为控件名，值为IconInfo类型，存储控件的相关信息。
        public Dictionary<string, Control>
            controlIconDic = new Dictionary<string, Control>();
        //存储可扩展硬件连接在USB口的位置信息
        public Dictionary<string, string> dicHard_USB = new Dictionary<string, string>();
        //[2021/1/30] 存储所有内置 修改
        //[2021/2/1] 不能存储所有内置控件 应该这里获取内置控件
        //并不能包含while循环这样的控件 所以还是应该存储所有外设
        //public ArrayList device_Inner = new ArrayList();
        public ArrayList notInner_device = new ArrayList();
        //public Dictionary<string, int> control_Cnt = new Dictionary<string, int>();
        //存储“Config.json”文件的所有信息
        public Config config = null;
        //【2021/1/30 存储所有外设控件以及该控件在图形化编程面板的个数】
        public Dictionary<string, int> control_Cnt = new Dictionary<string, int>();
        ///==============================================================================
        #endregion

        #region IconBoxInfo类的方法
        ///==============================================================================
        public LoadConfig()
        {
        }

        //从图标控件库中导出图标控件信息
        public bool LoadIconBoxInfoFromIconBoxBase()
        {
            try
            {
                //加载“Config.json”文件
                string configContent = File.ReadAllText(Globaldefine.exePath + @"Config\Config.json");
                config = JsonConvert.DeserializeObject<Config>(configContent);
                //解析出控件的信息至字典controlIconDic
                foreach (Control c in config.controls)
                {
                    this.controlIconDic.Add(c.name, c);
                    //[2021/2/1] 获取所有外设存储到notInner_device中
                    if (c.hard_type != null && c.hard_type != ""
                     && c.hard_type.Contains("inner") == false
                     )//若具备硬件类型且不是内置硬件即是外设
                        this.notInner_device.Add(c.name);
                }
                //解析出可扩展硬件连接信息至字典dicHard_USB
                foreach (var v in Globaldefine.loadConfig.controlIconDic)
                {
                    if (v.Value.hard_type != null && v.Value.hard_type != "" && v.Value.hard_type.Contains("inner") == false)//若具备硬件类型且不是内置硬件
                    {
                        dicHard_USB.Add(v.Key, "");
                    }
                }
                foreach (var name in Globaldefine.loadConfig.dicHard_USB.Keys)
                {
                    control_Cnt.Add(name, 0);//加入控件名，其个数初始化为0
                }
                return true;
            }
            catch { return false; }
        }
        ///==============================================================================
        #endregion

    }
}
